package com.yn.common.utils.web;

import com.yn.common.utils.ex.BaseErrorEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * @description: 封装后的返回对象
 * @author: 叶甯
 * @create: 2022/10/13 16:16
 * @since: 1.0.0
 * @copyright (C), 2022, https://gitee.com/johnny .All rights reserved.
 */
@Data
@ApiModel(description = "封装后的返回对象")
public class R {
    /* 返回码 */
    @ApiModelProperty("返回码，正常为0")
    private int code;
    /* 返回数据 */
    @ApiModelProperty("返回的数据")
    private Object data;
    /* 返回信息 */
    @ApiModelProperty("返回的提示信息")
    private String message;


    public static R success() {
        R r = new R();
        r.setCode(BaseErrorEnum.SUCCESS.getCode());
        r.setMessage(BaseErrorEnum.SUCCESS.getMessage());
        return r;
    }

    public static R error() {
        R r = new R();
        r.setCode(BaseErrorEnum.ERROR.getCode());
        r.setMessage(BaseErrorEnum.ERROR.getMessage());
        return r;
    }

    public static R codeMessage(BaseErrorEnum errorCode) {
        R r = new R();
        r.setCode(errorCode.getCode());
        r.setMessage(errorCode.getMessage());
        return r;
    }

    public R data(Object data) {
        this.setData(data);
        return this;
    }

    public R message(String message) {
        this.setMessage(message);
        return this;
    }

    public R code(int code) {
        this.setCode(code);
        return this;
    }

}
